import SvgIcon from '@/components/SvgIcon/SvgIcon';
import { ExtendedMenuItem } from '@/types';

export function transformSvgComponent(menus: ExtendedMenuItem[]): ExtendedMenuItem[] {
  if (!menus || menus.length === 0) return [];
  return menus.map(menu => {
    return {
      ...menu,
      icon: typeof menu.icon === 'string' ? <SvgIcon name={menu.icon} /> : menu.icon,
      children: menu.children ? transformSvgComponent(menu.children) : undefined,
    };
  });
}

export function findItemByPath(
  menus: ExtendedMenuItem[],
  path: string,
  parent?: ExtendedMenuItem | null, // 修改参数类型，允许 undefined 或 null
): ExtendedMenuItem | ExtendedMenuItem[] | null {
  // 明确返回值类型
  if (!menus || menus.length === 0) return null; // 空数组时返回 null 而非 []，与返回值类型一致
  for (const menu of menus) {
    if (menu.fullpath === path) {
      if (menu.children) {
        return menu.children; // 返回子菜单数组
      }
      return parent?.children || [menu]; // 简化判断逻辑
    }
    if (menu.children) {
      const result = findItemByPath(menu.children, path, menu);
      if (result) return result; // 找到结果时才返回，避免提前终止循环
    }
  }
  return null; // 遍历完未找到时返回 null
}
